// @ts-ignore
import mitt from 'mitt';
// @ts-ignore
import type { Emitter } from 'mitt';
interface MittType {
  event_name: any;
}
const emitter: Emitter<MittType> = mitt<MittType>();

const emitObj:{ [key: string]: () => void } = {};
// 开启监听事件
export const emitterOn = (eventName:string, eventFunc:any) => {
  if (emitObj[eventName]) {
    throw(new Error('监听事件已存在，请先关闭监听事件！'));
  }
  emitter.on(eventName, eventFunc);
  emitObj[eventName] = eventFunc;
};

// 关闭监听事件
export const emitterOff = (eventName:string) => {
  emitter.off(eventName, emitObj[eventName]);
  delete emitObj[eventName]
};

// 触发监听事件
export const emitterEmit = (eventName:string, data?:any) => {
  emitter.emit(eventName, data);
};
